/*
 * Copyright (c) 2015 Messcat. All rights reserved.
 */
package cc.messcat.dao.system;

import cc.messcat.common.constant.CommonRedisConstant;
import cc.messcat.common.dao.BaseDaoImpl;
import cc.messcat.common.util.CollectionUtil;
import cc.messcat.entity.area.Area;
import cc.messcat.entity.system.McUser;
import org.apache.shiro.SecurityUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author Microcat
 * @version 1.0
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<McUser> implements UserDao {

	@Override
	public McUser getUserIgnorePassword(Long id) {
		McUser user = get(id);
		user.setPassword(null);
		return user;
	}

	@Override
	public McUser getUserByUsername(String username) {
		List<McUser> list = getCurrentSession().createCriteria(McUser.class)
				.add(Restrictions.eq("username", username)).list();
		return CollectionUtil.isListNotEmpty(list) ? list.get(0) : null;
	}

	public McUser getCurrentMcUser(){
		String username = (String) SecurityUtils.getSubject().getSession()
				.getAttribute(CommonRedisConstant.REDIS_KEY_LOGIN_USER);
		return getUserByUsername(username);
	}

//	@Override
//	public List<McUser> listByRoleArea(Area area) {
//		Criteria criteria = getCurrentSession().createCriteria(McUser.class);
//		criteria.createAlias("roles", "roles");
//		criteria.add(Restrictions.eq("roles.areaId", area.getId()));
//		List<McUser> list = criteria.list();
//		return list;
//	}

	@Override
	public List<McUser> listByUserArea(Area area) {
		Criteria criteria = getCurrentSession().createCriteria(McUser.class);
		criteria.add(Restrictions.eq("areaId", area.getId()));
		List<McUser> list = criteria.list();
		return list;
	}

    @Override
    public List<Map<String,Object>> listByIDs(String praise) {
        return getCurrentSession().createSQLQuery(" select id as id ,username as username ,real_name as realName " +
                " from mc_user where id in("+ praise+")")
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
    }

}